package org.glorylight.hacks.social.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.glorylight.hacks.social.data.PictureData;
import org.glorylight.hacks.social.model.Picture;
import org.glorylight.hacks.social.util.ModelHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class PictureService extends BaseService {
	private static final Logger log = LoggerFactory.getLogger(PictureService.class);

	@Transactional(readOnly = false)
	public void addPicture(PictureData data) throws Exception {
		if (log.isDebugEnabled())
			log.debug("adding new picture for: {}", data.getTitle());
		add(ModelHelper.convertPictureData(data));
	}
	
	@Transactional(readOnly = false)
	public void deletePicture(PictureData data) throws Exception {
		if (log.isDebugEnabled())
			log.debug("delete picture for: {}", data.getId());
		Picture pic = findPicture(data.getId());
		delete(pic);
	}
	
	@Transactional(readOnly = false)
	public PictureData updatePicture(PictureData data) throws Exception {
		log.debug("updating picture title and description");
		Picture pic = findPicture(data.getId());
		pic.setTitle(data.getTitle());
		pic.setDescription(data.getDescription());
		update(pic);
		return ModelHelper.convertPicture(pic);
	}

	private Picture findPicture(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve picture: {}", id);
		return em.find(Picture.class, id);
	}

	public PictureData find(UUID id) {
		if (log.isDebugEnabled())
			log.debug("retrieve picture: {}", id);
		Picture data = findPicture(id);
		if (data != null)
			return ModelHelper.convertPicture(data);
		else
			return null;
	}
	
	public List<PictureData> findAll() {
		if (log.isDebugEnabled())
			log.debug("find all Picture");
		List<Picture> result = em.createQuery("from Picture", Picture.class).getResultList();
		List<PictureData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<PictureData>();
			for (Picture u : result)
				datas.add(ModelHelper.convertPicture(u));
		}
		
		return datas;
	}
	
	public List<PictureData> findAllByUser(String userId) {
		if (log.isDebugEnabled())
			log.debug("find all pictures");
		List<Picture> result = em.createNamedQuery("pictures-by-user", Picture.class)
				.setParameter("userId", userId).getResultList();
		List<PictureData> datas = new ArrayList<PictureData>();
		if (result.size() > 0){
			for (Picture u : result)
				datas.add(ModelHelper.convertPicture(u));
		}
		
		return datas;
	}
	
	public List<PictureData> findAllByUserDate(String userId, Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all books by user date");
		List<Picture> result = em.createNamedQuery("pictures-by-user-date", Picture.class)
				.setParameter("userId", userId)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<PictureData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<PictureData>();
			for (Picture u : result)
				datas.add(ModelHelper.convertPicture(u));
		}
		
		return datas;
	}
	
	public List<PictureData> findAllByDate(Date startDate, Date endDate) {
		if (log.isDebugEnabled())
			log.debug("find all pictures by date");
		List<Picture> result = em.createNamedQuery("pictures-by-date", Picture.class)
				.setParameter("startDate", startDate)
				.setParameter("endDate", endDate).getResultList();
		List<PictureData> datas = null;
		if (result.size() > 0){
			datas = new ArrayList<PictureData>();
			for (Picture u : result)
				datas.add(ModelHelper.convertPicture(u));
		}
		return datas;
	}
}
